package com.sigmazero13.MKDummy.activites;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.sigmazero13.MKDummy.R;
import com.sigmazero13.MKDummy.components.ImageSelector;
import com.sigmazero13.MKDummy.components.TacticsSelector;
import com.sigmazero13.MKDummy.models.ImageValuePair;
import com.sigmazero13.MKDummy.models.ManaColor;

public class NewRoundDialog extends Dialog {

	OnDismissListener dismissListener;
	ImageSelector<ManaColor> crystalSelector;
	ImageSelector<ManaColor> cardSelector;
	TacticsSelector tacticsSelector;
	boolean tacticsOnly = false;
	Button cancel;
	Context mContext;
	
	public NewRoundDialog(Context context, OnDismissListener dismissListener) {
		super(context);
		mContext = context;
		this.dismissListener = dismissListener;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_round_dialog);
		this.setTitle(R.string.new_round_dialog_title);
		
		List<ImageValuePair<ManaColor>> pairs = new ArrayList<ImageValuePair<ManaColor>>();
	    
		crystalSelector = (ImageSelector<ManaColor>)findViewById(R.id.crystal_selector);
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.crystal_red), ManaColor.RED));
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.crystal_blue), ManaColor.BLUE));
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.crystal_green), ManaColor.GREEN));
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.crystal_white), ManaColor.WHITE));	      
	    crystalSelector.setImageValues(pairs);
	    
	    cardSelector = (ImageSelector<ManaColor>)findViewById(R.id.card_selector);
	    pairs.clear();
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.card_red), ManaColor.RED));
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.card_blue), ManaColor.BLUE));
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.card_green), ManaColor.GREEN));
	    pairs.add(new ImageValuePair<ManaColor>(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.card_white), ManaColor.WHITE));
	    cardSelector.setImageValues(pairs);

	    tacticsSelector = (TacticsSelector)findViewById(R.id.tactics_selector); 

		Button ok = (Button)findViewById(R.id.dialog_ok);
		ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismissListener.onDismiss(NewRoundDialog.this);
				dismiss();
			}
		});
		
		cancel = (Button)findViewById(R.id.dialog_cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancel();
			}
		});
	}
	
	@Override
	protected void onStart() {
		LinearLayout tacticsLayout = (LinearLayout)findViewById(R.id.tactics_layout);
		LinearLayout cardCrystalLayout = (LinearLayout)findViewById(R.id.card_crystal_layout);
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
	    if (prefs.getBoolean("enable_tactics", false)) {
	    	tacticsLayout.setVisibility(View.VISIBLE);
	    	if (prefs.getBoolean("reset_tactics", false)) {
	    		tacticsSelector.resetTactics();
	    	}
	    } else {
	    	tacticsLayout.setVisibility(View.GONE);
	    }
	    
	    int visibility = View.VISIBLE;
	    if (tacticsOnly) {
	    	visibility = View.GONE;
	    }
    	cardCrystalLayout.setVisibility(View.GONE);
    	cancel.setVisibility(View.GONE);
	}
	
	public void prepare(Bundle args) {
		if (args == null) {
			tacticsOnly = false;
		} else {
			tacticsOnly = args.getBoolean(MKDummyPlayer.TACTICS_ONLY, false);
		}
	}
	
	public ManaColor getNewCardColor() {
		if (tacticsOnly) {
			return null;
		} else {
			return cardSelector.getSelectedValue();
		}
	}
	
	public ManaColor getNewCrystalColor() {
		if (tacticsOnly) {
			return null;
		} else {
			return crystalSelector.getSelectedValue();
		}
	}
	
	public Set<Integer> getAvailableTactics() {
		return tacticsSelector.getAvailableValues(); 
	}
	
	public void selectTactic(int tactic) {
		tacticsSelector.selectTactic(tactic);
	}
}
